# -*- coding: utf-8 -*-
__author__ = 'SlovEnt'
__date__ = '2018/10/17 14:31'
__version__ = '1.0'

import os

def get_param_info(ConfigFile):

    if os.path.isfile(ConfigFile) is False:
        raise Exception("错误，全局参数配置文件不存在")

    paramInfo = {}
    for line in open(ConfigFile,"r",encoding= 'UTF-8'):
        if line != "\n" :
            info = line.strip("\n")
            # 首字符为 # ; 等符号 视为注释
            if info.strip()[0] != "#" and info.strip()[0] != ";" and info.strip()[0] != "[" :
                # print(info.strip()[0])
                info = info.split("=")
                if len(info) == 2:
                    paramName = info[0].strip()
                    paramValue = info[1].strip()
                    paramInfo[paramName] = paramValue
    return paramInfo

def rtn_parainfo():
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    CONFIG_INFO_FILE = r"%s/%s" % (BASE_DIR, "Config.ini")
    PARAINFO = get_param_info(CONFIG_INFO_FILE)
    return PARAINFO

def rtn_db_param_info(mySqlConn, paramClsList=[]):
    if len(paramClsList) > 0:
        rtnParamList = ""
        for paramCls in paramClsList:
            if rtnParamList == "":
                rtnParamList = f"{paramCls}"
            else:
                rtnParamList = f"{rtnParamList},{paramCls}"
        strSql = f"select param_name, param_en_name, param_value from system_sysparam where 0=0 and param_cls in ({rtnParamList}) order by param_en_name"
    else:
        strSql = "select param_name, param_en_name, param_value from system_sysparam order by param_en_name"
    rtnDatas = mySqlConn.query(strSql, "y")
    DB_PARAM_INFO = {}
    for rtnData in rtnDatas:
        DB_PARAM_INFO[rtnData["PARAM_EN_NAME"]] = rtnData["PARAM_VALUE"]
    return DB_PARAM_INFO

